﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using NDK.Motion.BLL;

namespace NDK.Frm
{
    public partial class Frm_OmronPLCIO : Form
    {
        Thread Thread1 = null;
        Dictionary<string, string> DicPLCAddress = new Dictionary<string, string>();
        bool IsRun = true;
        public Frm_OmronPLCIO()
        {
            InitializeComponent();
        }

        private void Frm_OmronPLCIO_Load(object sender, EventArgs e)
        {
            dgvLine1Data.Rows.Clear();

            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "上料机器人_左穴放料到位", "stCom[0].PLC_PC.LdInPos", "", "上料机器人_左穴要料请求", "stCom[0].PC_PLC.LdReq", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "上料机器人_左穴放料完成", "stCom[0].PLC_PC.LdCompAck", "", "上料机器人_左穴允许离开", "stCom[0].PC_PLC.LdComp", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "上料机器人_右穴放料到位", "stCom[1].PLC_PC.LdInPos", "", "上料机器人_右穴要料请求", "stCom[1].PC_PLC.LdReq", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "上料机器人_右穴放料完成", "stCom[1].PLC_PC.LdCompAck", "", "上料机器人_右穴允许离开", "stCom[1].PC_PLC.LdComp", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "上料机器人_上料定位拍照请求", "stModule[0].PLC_PC.B[19]", "", "上料机器人_上料定位拍照完成", "stModule[0].PC_PLC.B[19]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "下料机器人_左穴取料到位", "stCom[0].PLC_PC.UldInPos", "", "下料机器人_左穴下料请求", "stCom[0].PC_PLC.UldReq", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "下料机器人_左穴取料完成", "stCom[0].PLC_PC.UldCompAck", "", "下料机器人_左穴允许离开", "stCom[0].PC_PLC.UldComp", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "下料机器人_右穴取料到位", "stCom[1].PLC_PC.UldInPos", "", "下料机器人_右穴下料请求", "stCom[1].PC_PLC.UldReq", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "下料机器人_右穴取料完成", "stCom[1].PLC_PC.UldCompAck", "", "下料机器人_右穴允许离开", "stCom[1].PC_PLC.UldComp", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "PLC心跳", "stLocal_UpDn[0].Heartbeat", "", "运行按钮", "stCom[0].B[0]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "PLC提示类报警", "stCom[2].B[6]", "", "停止", "stCom[0].B[1]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "PLC轻报警", "stCom[2].B[7]", "", "复位", "stCom[0].B[2]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "PLC重报警", "stCom[2].B[8]", "", "手自动", "stCom[0].B[3]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "PLC安全门打开", "stCom[2].B[9]", "", "PLC在线", "stCom[0].OnLine", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "PLC请求清料", "stCom[2].B[11]", "", "心跳", "stCom[0].HeartBeat", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "PLC允许PC载台回原点", "stCom[2].PLC_PC.Saftey", "", "PC安全门打开", "stCom[0].B[6]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "请求光源打开", "stCom[2].B[12]", "", "GRR模式中", "stCom[0].B[7]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "", "", "", "点检模式中", "stCom[0].B[8]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "", "", "", "GRR/点检开始", "stCom[0].B[9]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "", "", "", "屏蔽1号治具", "stCom[0].B[10]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "", "", "", "屏蔽2号治具", "stCom[0].B[11]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "", "", "", "屏蔽3号治具", "stCom[0].B[12]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "", "", "", "屏蔽4号治具", "stCom[0].B[13]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "", "", "", "上料机器人_左穴屏蔽模式", "stCom[0].B[14]", "", "False" });
            dgvLine1Data.Rows.Add(new object[] { dgvLine1Data.RowCount + 1, "", "", "", "上料机器人_右穴屏蔽模式", "stCom[0].B[15]", "", "False" });

            for (int i = 0; i < dgvLine1Data.RowCount; i++)
            {
                dgvLine1Data.Rows[i].Cells[7].ReadOnly = false;
            }

            Thread1 = new Thread(DisplayIO);
            Thread1.IsBackground = true;
            Thread1.Start();
        }

        public void DisplayIO()
        {
            while (IsRun)
            {
                try
                {
                    dgvLine1Data.Invoke(new Action(() =>
                    {
                        if (ckbIO.Checked && Machine.OmronPLC_Hearb.ConnectStatus)
                        {
                            for (int i = 0; i < dgvLine1Data.RowCount; i++)
                            {
                                if (dgvLine1Data.Rows[i].Cells[1].Value.ToString().Length > 5)
                                    dgvLine1Data.Rows[i].Cells[3].Style.BackColor = Machine.OmronPLC_Hearb.ReadBool(dgvLine1Data.Rows[i].Cells[2].Value.ToString()) == true ? Color.Green : Color.White;
                                if (dgvLine1Data.Rows[i].Cells[4].Value.ToString().Length > 5)
                                    dgvLine1Data.Rows[i].Cells[6].Style.BackColor = Machine.OmronPLC_Hearb.ReadBool(dgvLine1Data.Rows[i].Cells[5].Value.ToString()) == true ? Color.Green : Color.White;
                            }
                        }
                    }));
                }
                catch { }
                Thread.Sleep(100);
            }
        }

        private void dgvLine1Data_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 7 && e.RowIndex >= 0)
            {
                Machine.OmronPLC_Hearb.WriteBool(dgvLine1Data.Rows[e.RowIndex].Cells[5].Value.ToString(), dgvLine1Data.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True");
            }
        }

        private void Frm_OmronPLCIO_FormClosing(object sender, FormClosingEventArgs e)
        {
            IsRun = false;
            Thread.Sleep(1000);
        }

        private void dgvLine1Data_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 7 && e.RowIndex >= 0)
            {
                if (MessageBox.Show($"是否将{dgvLine1Data.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()}改为{!Convert.ToBoolean(dgvLine1Data.Rows[e.RowIndex].Cells[e.ColumnIndex].Value)}", "PLC发送值", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    if (dgvLine1Data.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True")
                        dgvLine1Data.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "False";
                    else
                        dgvLine1Data.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "True";
                }
            }
        }
    }
}
